package com.gduf.lsm.controller;

import com.gduf.lsm.common.Result;
import com.gduf.lsm.entity.JoinActivity;
import com.gduf.lsm.pojo.dto.JoinStatusDTO;
import com.gduf.lsm.pojo.vo.JoinActivityShowVO;
import com.gduf.lsm.service.JoinActivityService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 公益活动报名表前端操作接口
 **/
@RestController
@RequestMapping("/joinActivity")

public class JoinActivityController {

    @Resource
    private JoinActivityService joinActivityService;

    /**
     * 新增
     * @param joinActivity
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody JoinActivity joinActivity) {
        System.out.println("-------------**------"+joinActivity);
        joinActivityService.add(joinActivity);
        return Result.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        joinActivityService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        joinActivityService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     * @param joinActivity
     * @return
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody JoinActivity joinActivity) {
        joinActivityService.updateById(joinActivity);
        return Result.success();
    }

    /**
     * 修改活动状态
     * @param joinStatusDTO
     * @return
     */
    @PutMapping("/updateStatus")
    public Result updateStatus(@RequestBody JoinStatusDTO joinStatusDTO){
        joinActivityService.updateStatus(joinStatusDTO);
        return Result.success();
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        JoinActivity joinActivity = joinActivityService.selectById(id);
        return Result.success(joinActivity);
    }

    /**
     * 查询所有
     * @param joinActivity
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll(JoinActivity joinActivity ) {
        List<JoinActivity> list = joinActivityService.selectAll(joinActivity);
        return Result.success(list);
    }

    /**
     * 按用户ID查找所有报名的活动
     * 返回的实体是整理过的映射VO
     * @param userId
     * @return
     */
    @GetMapping("/selectByUserId/{userId}")
    public Result selectByUserId(@PathVariable Integer userId){
        List<JoinActivityShowVO> list=joinActivityService.selectByUserId(userId);
        return Result.success(list);
    }

    /**
     * 分页查询
     * @param joinActivity
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/selectPage")
    public Result selectPage(JoinActivity joinActivity,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        //PageInfo<JoinActivity> page = joinActivityService.selectPage(joinActivity, pageNum, pageSize);
        PageInfo<JoinActivityShowVO> page = joinActivityService.selectPage(joinActivity, pageNum, pageSize);
        return Result.success(page);
    }

}